"""
Copyright (c) 2023 - present SKbioinfo
"""

from apps.NGS_API.connector.db_common import query_info
from flask_restful import Resource
from flask_login import login_required
from apps.NGS_API.charts.common_query import *
from apps.NGS_API.common.db_table_config import *
from apps.NGS_API.utils.log_helper import lg

'''
    INTERFACE: monthly report of samples
    DATA_URL: /api/charts/lineCharts/sample
    TEMPLATE: /template/home/laboratory/NGS/NGS_Samples_search.html
    JAVASCRIPT: /static/interface/interfaceSampleAmount.js
'''
class interfaceProjectPie(Resource):
    def get(self):
        try:
            project_infos_sql = '''
                SELECT
                    project_id,
                    status
                FROM {table_name}
            '''.format(
                table_name = project_db["name"]
            )
            projects = query_info(project_infos_sql)
            '''
                count complete projects 
            '''
            project_total_count={}
            project_complete = 0
            project_pending = 0
            for project in projects:
                if int(project[1]) == 0:
                    project_pending += 1
                elif int(project[1]) == 1:
                    project_complete += 1
            project_total_count["projects"] = [project_complete,project_pending]
            return project_total_count
        except Exception as e:
            lg.error(e)
            
'''
    INTERFACE: monthly report of samples
    DATA_URL: /api/charts/lineCharts/sample
    TEMPLATE: /template/home/laboratory/NGS/NGS_Samples_search.html
    JAVASCRIPT: /static/interface/interfaceSampleAmount.js
'''
class interfaceExtractionPie(Resource):
    @login_required
    def get(self):
        try:
            extraction_total_sql=total_query_sql(
                col="extraction_id",
                table_name="ngs_extraction"
            )
            extractions = query_info(extraction_total_sql)
            tmp_pie={}
            for extraction in extractions:
                tmp_pie[extraction[0]] = {"status":1}
            extraction_total_count={}
            extraction_complete = 0
            extraction_pending = 0
            for id,infos in tmp_pie.items():
                if infos["status"]==1:
                    extraction_complete += 1
                elif infos["status"]==0:
                    extraction_pending += 1 
            extraction_total_count["extractions"] = [extraction_complete,extraction_pending]
            return extraction_total_count
        except Exception as e:
            lg.error(e)